package com.example.project.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.example.project.annotations.PassToken;
import com.example.project.annotations.UserLoginToken;
import com.example.project.common.Result;
import com.example.project.entity.TableA;
import com.example.project.entity.TableB;
import com.example.project.service.TableBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Ethan on 2022-08-25 18:19
 */
@PassToken
@RestController
@RequestMapping("tableB")
@Slf4j
public class TableBController {
    @Autowired
    private TableBService tableBService;

    /**
     * 导入文件，并加入到数据库
     * @param file
     * @return
     */
    @PostMapping("/add")
    @PassToken
    public Result<List<TableB>> addLists(@RequestPart("file") MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        try {
            List<TableB> list = ExcelImportUtil.importExcel(
                    file.getInputStream(),
                    TableB.class, params);
            for (TableB b : list) {
                tableBService.saveTableB(b);
                log.info(b.toString());
            }
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("错误！！！");
        }
    }

    /**
     * 查找所有
     * @return
     */
    @GetMapping("/findAll")
    @PassToken
    public Result<List<TableB>> findAll() {
        List<TableB> all = tableBService.findAll();
        return Result.success(all);
    }

    /**
     * 增添一个
     * @param tableB
     * @return
     */
    @PostMapping("/addOne")
    @PassToken
    public Result<String> addOne(@RequestBody TableB tableB) {
        tableBService.saveTableB(tableB);
        return Result.success("成功加入!!!");
    }

    /**
     * 更新操作
     * @param tableB
     * @return
     */
    @PostMapping("update")
    @PassToken
    public Result<String> update(@RequestBody TableB tableB) {
        log.info(tableB.toString());
        tableBService.update(tableB);
        return Result.success("成功修改!!!");
    }

    @GetMapping("/out")
    @PassToken
    public void out(ModelMap map,
                    HttpServletRequest request,
                    HttpServletResponse response) {
        // 从数据库获取数据
        List<TableB> bList = tableBService.findAll();
        // 设置输出参数
        ExportParams params = new ExportParams("B表", "sheet1", ExcelType.XSSF);
        // 日志文件
        for (TableB b : bList) {
            log.info(b.toString());
        }
        // 将文件返回到客户端接收
        map.put(NormalExcelConstants.DATA_LIST, bList);
        map.put(NormalExcelConstants.CLASS, TableB.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "tableBTest");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
}
